package cn.com.mjsoft.framework.persistence.core.support;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * 执行数据库uodate后的状态bean，此状态和各数据库特性相关
 * 
 * @author mjsoft
 * 
 */
public class UpdateState
{

    public static final Long NO_VALUE = new Long( "-1" );

    private int row = NO_VALUE.intValue();// 更新的行数

    private long key = NO_VALUE.longValue();// 执行一次insert后自增的主键

    public UpdateState()
    {

    }

    public UpdateState( int row )
    {
        this.row = row;
    }

    public UpdateState( long key )
    {
        this.key = key;
    }

    public UpdateState( int row, long key )
    {
        this.row = row;
        this.key = key;
    }

    public long getKey()
    {
        return key;
    }

    public void setKey( long key )
    {
        this.key = key;
    }

    public int getRow()
    {
        return row;
    }

    public void setRow( int row )
    {
        this.row = row;
    }

    public boolean equals( Object object )
    {
        if( !( object instanceof UpdateState ) )
        {
            return false;
        }
        UpdateState rhs = ( UpdateState ) object;
        return new EqualsBuilder().append( this.key, rhs.key ).append(
            this.row, rhs.row ).isEquals();
    }

    public int hashCode()
    {
        return new HashCodeBuilder( -1563548323, 1232661865 ).append( this.key )
            .append( this.row ).toHashCode();
    }

    /**
     * 若增加动作成功,返回true
     * 
     * @return
     */
    public boolean haveKey()
    {
        return !( key < 0 );

    }

    /**
     * @see java.lang.Object#toString()
     */
    public String toString()
    {
        return new ToStringBuilder( this ).append( "key", this.key ).append(
            "row", this.row ).toString();
    }

}
